package com.shujia;

import com.mysql.jdbc.Driver;

import java.sql.*;

public class Code0MysqlCon {
    public static void main(String[] args) throws SQLException {
        // 1.注册驱动
        /**
         * registerDriver(java.sql.Driver driver)
         */
        DriverManager.registerDriver(new Driver());

        // 2.获取连接
        /*
            Connection getConnection(String url,String user, String password)

            URL:格式: jdbc:mysql://IP:PORT/数据库?参数名1=参数值1&参数名2=参数值2
            jdcb: 主协议
            mysql: 次级协议，表示连接MySQL数据库
            IP: Mysql所在服务器地址
            PORT: Mysql端口号(默认3306)

         */

        String url = "jdbc:mysql://192.168.20.90:3306/bigdata19?useSSL=false&useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, user, password);

        /**
         * 连接异常：
         *      Caused by: java.net.ConnectException: Connection timed out: connect
         *      解决方式：
         *          1.排查参数填写问题
         *          2.排查服务器中MySQL是否运行
         *          3.排查MySQL的权限问题及防火墙问题
         * 认证：
         *      not using SSL the verifyServerCertificate property is set to 'false'.
         *      解决方式：
         *          添加 useSSL=false
         */
        System.out.println(connection);
        // 3.获取操作对象
        Statement statement = connection.createStatement();
        // 4.执行SQL
        String sql = "select * from emp";

        ResultSet resultSet = statement.executeQuery(sql);

        // 5.获取执行结果
        while (resultSet.next()) {

            String ename = resultSet.getString("ENAME");
            String EMPNO = resultSet.getString("EMPNO");
            String JOB = resultSet.getString("JOB");
            int SAL = resultSet.getInt("SAL");
            String DEPTNO = resultSet.getString("DEPTNO");

            System.out.println(ename + "\t" + EMPNO + "\t" + JOB + "\t" + SAL + "\t" + DEPTNO + "\t");
        }


        // 6.关闭资源
        connection.close();

    }
}
